package toolsUseCourse.JSON.jsonfileio;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import json.praseJSON.AbstractParseJSONFile;
import utils.date.DateTimeUtils;

import java.io.IOException;
import java.util.HashMap;

import static utils.CloneUtils.deepCloneObject;

public class FirstExampleParseJSONFile extends AbstractParseJSONFile {
//    在其他电脑上运行的时候，获取文件路径方法：选中json文件-》Ctrl+shift+c（复制json文件路径）-》替换掉对应的变量
    /**
     * 比如获取ORIGIN_PATH
     * 选中wechat_interface.postman_collection.json文件-》Ctrl+Shift+C（复制到json文件的路径）-》替换掉ORIGIN_PATH
     */
//    notes：不同电脑，项目路径不同，文件路径也会跟着变，不改路径，会报错，failed to open file
    public static String ORIGIN_PATH = "D:\\personal\\project\\springboot-demo\\framework_usage_case\\JSON\\wechat_interface.postman_collection.json";
//    public static String DESTINATION_PATH = "D:\\work\\xikuan\\biz\\comprehensiveModule\\src\\prase_json_file\\destination.postman_collection.json";
    public static String DESTINATION_PATH = "D:\\personal\\project\\springboot-demo\\framework_usage_case\\JSON\\destination.postman_collection.json";






    public static void main(String[] args) throws IOException {
//      实例化对象，调用ReadJSONFile，把jsonFile转换为JSONObject
        FirstExampleParseJSONFile firstExampleParseJSONFile = new FirstExampleParseJSONFile();
/*        String JsonContext = firstExampleParseJSONFile.ReadJSONFile(ORIGIN_PATH);
        JSONObject originJsonObject = JSONArray.parseObject(JsonContext);*/
        JSONObject originJsonObject = JSONArray.parseObject(firstExampleParseJSONFile.ReadJSONFile(ORIGIN_PATH)); // 等价于上面的两行代码

//      destinationJsonObject，最终要转换成的JSONObject
        JSONObject destinationJsonObject = new JSONObject();
//      info是不会改变的部分，直接put进去
        destinationJsonObject.put("info",originJsonObject.get("info"));
//      item的0不会变
        JSONArray item = originJsonObject.getJSONArray("item");
        JSONObject itemJsonObject = item.getJSONObject(0);
        destinationJsonObject.put("item",item);

//       item的0之后是会改变的部分，按照下列规则改变
//      获取当前年和月
        DateTimeUtils dateTimeUtils = new DateTimeUtils();
        HashMap currentDate= dateTimeUtils.calculateCurrentDate();

        Integer destinationSize = ((Integer.parseInt(currentDate.get("YEAR").toString())) -2018) * 12 + (Integer.parseInt(currentDate.get("MONTH").toString()));
        for (int itemIndex = 1; itemIndex < destinationSize; itemIndex++) {
//            item中的query和raw需要改变，其他部分不需要

//            item是一个数组，JSONArray
            JSONArray destinationItem = destinationJsonObject.getJSONArray("item");
            JSONObject previousDestinationItem = destinationItem.getJSONObject(itemIndex - 1);
//            JSONObject itemOfCurrentIndex = (JSONObject) previousDestinationItem.clone(); // 这是浅克隆

            JSONObject itemOfCurrentIndex =  deepCloneObject(previousDestinationItem); // 这是深克隆
//            JSONObject itemOfCurrentIndex =  (JSONObject)deepCloneObject(previousDestinationItem); // 这是深克隆

//            JSONObject itemOfCurrentIndex = previousDestinationItem; // 这是浅克隆

//            destinationItem.add(itemOfCurrentIndex);
//            itemOfCurrentIndex = destinationItem.getJSONObject(itemIndex);

/*            JSONObject clonedItemJsonObject = (JSONObject) originJsonObject
                    .getJSONArray("item")
                    .getJSONObject(0).clone();// 等价于上面的三行代码*/

//        获取query
            JSONArray query = itemOfCurrentIndex.getJSONObject("request")
                    .getJSONObject("url")
                    .getJSONArray("query");
            JSONObject bus_yearJsonObject = query.getJSONObject(1);
            String previous_bus_year = bus_yearJsonObject.getString("value");
//            修改bus_year
            String current_bus_year = String.valueOf(Integer.parseInt(previous_bus_year) + 1);
            if(0 == (itemIndex+1)%12){
                bus_yearJsonObject.put("value",current_bus_year);
            }
//            修改bus_month
            JSONObject bus_monthJsonObject = query.getJSONObject(2);
            String previous_bus_month = bus_monthJsonObject.getString("value");
            Integer current_bus_month = (Integer.parseInt(previous_bus_month) + 1) % 12;
            current_bus_month = current_bus_month.intValue();
            String current_bus_month_str = String.valueOf(current_bus_month == 0 ? 12 : current_bus_month);
            bus_monthJsonObject.put("value", current_bus_month_str);

//        获取raw
            JSONObject url = itemOfCurrentIndex.getJSONObject("request")
                    .getJSONObject("url");
            String previousRaw = url.getString("raw");
            String currentRaw = previousRaw;

            if(0 == (itemIndex+1)%12) {
//                currentRaw = previousRaw.replace("bus_year=" + previous_bus_year,"bus_year=" + bus_yearJsonObject.getString("value"));
                currentRaw = currentRaw.replace("bus_year=" + previous_bus_year,"bus_year=" + current_bus_year);
            }
            currentRaw = currentRaw.replace("bus_month=" + previous_bus_month,"bus_month=" + current_bus_month_str);

            url.put("raw",currentRaw);
            destinationItem.add(itemOfCurrentIndex);
        }


//        还没有写完，暂时不写入文件
//        把转换完成的destinationJsonObject写入jsonfile
        firstExampleParseJSONFile.WriterJsonObjectToJSONFile(DESTINATION_PATH,destinationJsonObject);

    }
}

/*

((com.alibaba.fastjson.JSONObject)((com.alibaba.fastjson.JSONArray)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((com.alibaba.fastjson.JSONArray)((HashMap.Node)destinationJsonObject.entrySet().toArray()[0]).getValue()).get(0)).entrySet().toArray()[0]).getValue()).entrySet().toArray()[4]).getValue()).entrySet().toArray()[3]).getValue()).get(1)).entrySet().toArray()[1]
((com.alibaba.fastjson.JSONObject)((com.alibaba.fastjson.JSONArray)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((com.alibaba.fastjson.JSONArray)((HashMap.Node)destinationJsonObject.entrySet().toArray()[0]).getValue()).get(0)).entrySet().toArray()[0]).getValue()).entrySet().toArray()[4]).getValue()).entrySet().toArray()[3]).getValue()).get(2)).entrySet().toArray()[1]
((com.alibaba.fastjson.JSONObject)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((com.alibaba.fastjson.JSONArray)((HashMap.Node)destinationJsonObject.entrySet().toArray()[0]).getValue()).get(0)).entrySet().toArray()[0]).getValue()).entrySet().toArray()[4]).getValue()).entrySet().toArray()[5]

((com.alibaba.fastjson.JSONObject)((com.alibaba.fastjson.JSONArray)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((com.alibaba.fastjson.JSONArray)((HashMap.Node)destinationJsonObject.entrySet().toArray()[0]).getValue()).get(12)).entrySet().toArray()[0]).getValue()).entrySet().toArray()[4]).getValue()).entrySet().toArray()[3]).getValue()).get(1)).entrySet().toArray()[1]
((com.alibaba.fastjson.JSONObject)((com.alibaba.fastjson.JSONArray)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((com.alibaba.fastjson.JSONArray)((HashMap.Node)destinationJsonObject.entrySet().toArray()[0]).getValue()).get(12)).entrySet().toArray()[0]).getValue()).entrySet().toArray()[4]).getValue()).entrySet().toArray()[3]).getValue()).get(2)).entrySet().toArray()[1]
((com.alibaba.fastjson.JSONObject)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((com.alibaba.fastjson.JSONArray)((HashMap.Node)destinationJsonObject.entrySet().toArray()[0]).getValue()).get(12)).entrySet().toArray()[0]).getValue()).entrySet().toArray()[4]).getValue()).entrySet().toArray()[5]

((com.alibaba.fastjson.JSONObject)((com.alibaba.fastjson.JSONArray)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((com.alibaba.fastjson.JSONArray)((HashMap.Node)destinationJsonObject.entrySet().toArray()[0]).getValue()).get(11)).entrySet().toArray()[0]).getValue()).entrySet().toArray()[4]).getValue()).entrySet().toArray()[3]).getValue()).get(1)).entrySet().toArray()[1]
((com.alibaba.fastjson.JSONObject)((com.alibaba.fastjson.JSONArray)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((com.alibaba.fastjson.JSONArray)((HashMap.Node)destinationJsonObject.entrySet().toArray()[0]).getValue()).get(11)).entrySet().toArray()[0]).getValue()).entrySet().toArray()[4]).getValue()).entrySet().toArray()[3]).getValue()).get(2)).entrySet().toArray()[1]
((com.alibaba.fastjson.JSONObject)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((com.alibaba.fastjson.JSONArray)((HashMap.Node)destinationJsonObject.entrySet().toArray()[0]).getValue()).get(11)).entrySet().toArray()[0]).getValue()).entrySet().toArray()[4]).getValue()).entrySet().toArray()[5]

((com.alibaba.fastjson.JSONObject)((com.alibaba.fastjson.JSONArray)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((com.alibaba.fastjson.JSONArray)((HashMap.Node)destinationJsonObject.entrySet().toArray()[0]).getValue()).get(24)).entrySet().toArray()[0]).getValue()).entrySet().toArray()[4]).getValue()).entrySet().toArray()[3]).getValue()).get(1)).entrySet().toArray()[1]
((com.alibaba.fastjson.JSONObject)((com.alibaba.fastjson.JSONArray)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((com.alibaba.fastjson.JSONArray)((HashMap.Node)destinationJsonObject.entrySet().toArray()[0]).getValue()).get(24)).entrySet().toArray()[0]).getValue()).entrySet().toArray()[4]).getValue()).entrySet().toArray()[3]).getValue()).get(2)).entrySet().toArray()[1]
((com.alibaba.fastjson.JSONObject)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((com.alibaba.fastjson.JSONArray)((HashMap.Node)destinationJsonObject.entrySet().toArray()[0]).getValue()).get(24)).entrySet().toArray()[0]).getValue()).entrySet().toArray()[4]).getValue()).entrySet().toArray()[5]

((com.alibaba.fastjson.JSONObject)((com.alibaba.fastjson.JSONArray)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((com.alibaba.fastjson.JSONArray)((HashMap.Node)destinationJsonObject.entrySet().toArray()[0]).getValue()).get(31)).entrySet().toArray()[0]).getValue()).entrySet().toArray()[4]).getValue()).entrySet().toArray()[3]).getValue()).get(1)).entrySet().toArray()[1]
((com.alibaba.fastjson.JSONObject)((com.alibaba.fastjson.JSONArray)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((com.alibaba.fastjson.JSONArray)((HashMap.Node)destinationJsonObject.entrySet().toArray()[0]).getValue()).get(31)).entrySet().toArray()[0]).getValue()).entrySet().toArray()[4]).getValue()).entrySet().toArray()[3]).getValue()).get(2)).entrySet().toArray()[1]
((com.alibaba.fastjson.JSONObject)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((HashMap.Node)((com.alibaba.fastjson.JSONObject)((com.alibaba.fastjson.JSONArray)((HashMap.Node)destinationJsonObject.entrySet().toArray()[0]).getValue()).get(31)).entrySet().toArray()[0]).getValue()).entrySet().toArray()[4]).getValue()).entrySet().toArray()[5]

* */
